package database
import (
	"fmt"
	"demo/models"
	"github.com/sirupsen/logrus"
	_ "github.com/go-sql-driver/mysql" 
	"xorm.io/xorm"
)

var engine *xorm.Engine
var xormErr error

func CreateEngine(Dbname, Dburl string) error {
	fmt.Println(Dbname, Dburl)
	engine, xormErr = xorm.NewEngine(Dbname, Dburl)
	if xormErr != nil {
		logrus.Errorln("Create engine failed:", xormErr)
		return xormErr
	}

	// 测试数据库连接
	if err := engine.Ping(); err != nil {
		logrus.Errorln("Database ping failed:", err)
		return err
	}
	logrus.Infoln("Database connection successful")

	logrus.Infoln("Starting Sync2")
	xormErr = engine.Sync2(new(models.Userinfo))
	if xormErr != nil {
		logrus.Errorln("Sync2 failed:", xormErr)
	} else {
		logrus.Infoln("Sync2 successful")
	}
	return xormErr
}

